package com.leo.foodmaster.admin.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leo.foodmaster.admin.system.entity.Dept;
import com.leo.foodmaster.admin.system.entity.Organ;
import com.leo.foodmaster.admin.system.service.IDeptService;
import com.leo.foodmaster.commons.entity.Result;
import com.leo.foodmaster.commons.entity.TreeSelect;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * <p>
 * 部门表 前端控制器
 * </p>
 *
 * @author Leo
 * @since 2021-07-15
 */
@RestController
@AllArgsConstructor
@Api(tags = "部门管理")
@RequestMapping("/system/dept")
public class DeptController {
    private final IDeptService deptService;


    @ApiOperation("条件分页查询部门列表")
    @GetMapping("/list")
    public Result<IPage<Dept>> getDeptPageList(
            @ApiParam(value = "当前页，从1开始", required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "每页记录数", required = true) @RequestParam Integer pageSize,
            @ApiParam(value = "公司Id") @RequestParam(required = false) Long organId,
            @ApiParam(value = "部门名称") @RequestParam(required = false) String name,
            @ApiParam(value = "部门状态") @RequestParam(required = false) String status,
            @ApiParam(value = "开始时间") @RequestParam(required = false) String beginTime,
            @ApiParam(value = "结束时间") @RequestParam(required = false) String endTime ) {
        IPage<Dept> pageList  = deptService.getOrganPageList(new Page<>(pageNum, pageSize), organId, name,
                status, beginTime, endTime);
        return Result.success(pageList);
    }

    @ApiOperation("查询所有部门树形列表")
    @GetMapping("/treeSelect")
    public Result<List<TreeSelect>> treeSelect(@ApiParam("公司Id")
                                               @RequestParam(required = false) Long organId) {
        List<TreeSelect> treeSelectList  = deptService.treeSelect(organId);
        return Result.success(treeSelectList);
    }

    @PostMapping
    @ApiOperation("新增部门")
    public Result<Object> addOrgan(@RequestBody Dept dept) {
        return deptService.addOrgan(dept);
    }

    @PutMapping
    @ApiOperation("修改部门")
    public Result<Object> updateDept(@RequestBody Dept dept) {
        return deptService.updateDept(dept);
    }

    @DeleteMapping("/{deptId}")
    @ApiOperation("删除部门")
    public Result<Object> deleteDept(@ApiParam("部门Id") @PathVariable String deptId) {
        return deptService.deleteDept(deptId);
    }

}

